package com.feeyo.groundservice.recordnfc.dlg

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.setViewTreeLifecycleOwner
import androidx.savedstate.setViewTreeSavedStateRegistryOwner
import com.blankj.utilcode.util.ScreenUtils
import com.feeyo.groundservice.base_util.CallbackWithData
import com.feeyo.groundservice.recordnfc.R
import com.feeyo.groundservice.recordnfc.activity.view.ResourceListMainView
import com.feeyo.groundservice.recordnfc.constant.ResourceType
import com.feeyo.groundservice.recordnfc.databinding.ViewCommonPopBinding
import com.lxj.xpopup.core.BottomPopupView

/**
 *作者：70893 on 2025-11-10
 *描述：
 */
internal class ResourceListPopView(context: Context) : BottomPopupView(context) {

    init {
        id = android.R.id.content
        setViewTreeLifecycleOwner(context as AppCompatActivity)
        setViewTreeSavedStateRegistryOwner(context as AppCompatActivity)
    }

    private lateinit var binding: ViewCommonPopBinding

    private val searchKey = mutableStateOf("")
    private var type: ResourceType? = null//录卡资源类型
    private var multiSelect: Boolean = false//是否多选
    private var listener: CallbackWithData<Any>? = null//回调
    private var data: ArrayList<String> = arrayListOf() //选择的老数据
    private val filterTask: ArrayList<String> = arrayListOf()//追加时已存在的任务需屏蔽

    override fun getImplLayoutId(): Int {
        return R.layout.view_common_pop
    }

    override fun onCreate() {
        super.onCreate()
        binding = ViewCommonPopBinding.bind(this.popupImplView)
        binding.popCp.apply {
            setContent {
                ResourceListMainView(
                    searchKey = searchKey,
                    resourceType = type,
                    multiSelect = multiSelect,
                    oldData = data,
                    filterTask = filterTask,
                    listener = listener,
                    onDismiss = { dismiss() }
                )
            }
        }
    }

    override fun getMaxHeight(): Int {
        return (ScreenUtils.getScreenHeight() * 0.8).toInt()
    }


    fun setType(type: ResourceType?) {
        this.type = type
    }

    fun setMultiSelect(multiSelect: Boolean) {
        this.multiSelect = multiSelect
    }

    fun setListener(listener: CallbackWithData<Any>) {
        this.listener = listener
    }

    fun setData(data: List<String>) {
        this.data.clear()
        this.data.addAll(data)
    }

    fun setFilterTask(taskId: List<String>) {
        this.filterTask.clear()
        this.filterTask.addAll(taskId)
    }

}